#include "../include/sound.h"
#include "../include/systimer.h"

static int _snd		=	0;
static int _signal		=	0;

static int _delayMax	=	0;
static int _delay		=	0;

static int _timeMax	=	0;
static int _time		=	0;

void playSound(int delay, int time)
{
	_delayMax	=	delay;
	_delay		=	0;
	_timeMax	=	time * 4;
	_time		=	0;
	_snd		=	1;

	/*while(_snd)
		_sound();*/
}

void playBlockSound(int delay, int time)
{
	playSound(delay, time);
	delay_ms(time);
}

void _sound(void)
{
	if(_snd)
	{
		if(++_delay	> _delayMax)
		{
			_delay	=	0;

			if(_signal)
			{
				IO0SET	=	P_13;
				_signal	=	0;
			}
			else
			{
				_signal	=	1;
				IO0CLR	=	P_13;
			}
		}

		if(++_time > _timeMax)
				_snd	=	0;
	}
}